package paintball;

import game.Constants;
import game.GameMap;
import game.GameObject;
import game.Player;
import game.Scoreboard;

import java.io.File;
import java.net.Socket;
import java.util.ArrayList;

import controllers.ProjectileController;

import org.json.JSONException;
import org.json.JSONObject;

import sound.OggPlayer;


public class PaintballPlayer extends Player {
	//Whether player is moving
	public boolean move;
	//Whether player has a powerup
	public boolean powerUp1 = false;
	public boolean powerUp2 = false;
	public boolean powerUp3 = false;
	//Whether player can shoot
	public boolean canShoot = true;
	//The current time between shots
	public int shootCounter = Constants.timeBetweenShots;
	//Counters for the powerups
	private int powerup1counter = 4;
	private int powerup2counter = 5;
	private int powerup3counter = 600;

	//The sound that plays when shooting
	File shootsound = new File("res/testshot.mid");

	public PaintballPlayer(Socket socket) {
		super(socket);
		this.setWidth(32);
		this.setHeight(32);
		this.setRespawnTime(300);
		this.setHealth(3);
	}

	@Override
	public String curImgPath() {
		switch (this.state) {
		case NORMAL:
			return "res/paintballplayer.gif";
		case DEAD:
			return "res/pldead.png";
		}
		return "";
	}

	@Override
	public void move(GameMap gameMap) {
		if (move) {
			super.move(gameMap);
			if (!canMoveWithoutGoingOutOfMap(gameMap.getWidth(),
					gameMap.getHeight())) {
				move = false;
			}
		}
	}

	/**
	 * An action that gets repeated by the game every cycle
	 */
	public void stepAction(){
		if(!canShoot){
			shootCounter --;
			if(shootCounter <= 0){
				shootCounter = Constants.timeBetweenShots;
				canShoot = true;
			}
		}
		if(powerUp3){
			powerup3counter--;
			if(powerup3counter <= 0){
				powerUp3 = false;
				powerup3counter = 600;
				OggPlayer.stopMusic();
			}
		}
	}
	
	
	/**
	 * @return Whether the player can shoot
	 */
	public boolean shoot(){
		if(this.state != State.DEAD){
			if(canShoot){
				PaintballBullet b = new PaintballBullet(this.getX(), this.getY(), this.getRotation(), this);

				ProjectileController.addProjectile(b);
				if (this.powerup1counter <= 0) {
					this.powerUp1 = false;
					this.powerup1counter = 4;
				}

				if (this.powerUp1) {
					PaintballBullet b1 = new PaintballBullet(this.getX(),
							this.getY(), this.getRotation() + 20, this);
					PaintballBullet b2 = new PaintballBullet(this.getX(),
							this.getY(), this.getRotation() - 20, this);
					ProjectileController.addProjectile(b1);
					ProjectileController.addProjectile(b2);
					this.powerup1counter--;
				}
				if(this.powerUp2){
					if(this.powerup2counter <= 0){
						this.powerUp2 = false;
						this.powerup2counter = 5;
					}
					this.shootCounter = 1;
					this.powerup2counter--;
				}
				//this.mp.startPlayer(shootsound, false);
				canShoot = false;
				OggPlayer.playMusic(2);
				return true;
			}
		}
		return false;
	}

	/**
	 * @param damage
	 *            - the amount of damage the hit does
	 */
	public void TakeHit(int damage, String nameSender) {
		this.setHealth(this.getHealth() - damage);
		if (getHealth() < 1) {
			Scoreboard.increaseScore(nameSender);
			this.setActive(false);
			this.state = State.DEAD;
		}
	}

	@Override
	public void postRespawn() {
		this.setHealth(3);
	}
	
	public void setInvulnerable(){
		OggPlayer.playMusic(1);
	}

	@Override
	public void collisionHandler(GameObject collider) {
		if(!collider.isActive()){
			return;
		}
		int modifier = 0;
		if (collider instanceof PaintballPowerup) {
			collider.setActive(false);
			modifier = ((PaintballPowerup)collider).getPowerUpID();
		}

		if(modifier != 0){
			try {
				JSONObject jOb = new JSONObject();
				jOb.put("Code", 1);
				jOb.put("Powerup", modifier);
				this.sendObject(jOb);
				System.out.println(jOb.toString());
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(powerUp3){
			if(collider instanceof PaintballPlayer){
				((PaintballPlayer)collider).TakeHit(999, this.getPlayerName());
			}
		}
	}

	/**
	 * Collision check with a single object
	 * @param go - The Game Object
	 */
	public void collisionCheck(GameObject go) {
		ArrayList temp = new ArrayList();
		temp.add(go);
		this.collisionCheck(temp);
	}
}
